package com.ktjy5.test;

import com.ktjy5.entity.StorageRecord;
import com.ktjy5.entity.Supplier;
import com.ktjy5.entity.SysUser;
import com.ktjy5.service.StorageRecordService;
import com.ktjy5.service.SupplierService;
import com.ktjy5.service.SysUserService;
import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

/**
 * @author 黄军艳
 * @version 1.0
 * @date 2024/12/9  10:57
 */
public class Main {
    Logger logger= LoggerFactory.getLogger(Main.class);
    @Test
    public void test() {
        ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
        SysUserService sysUserService = (SysUserService) context.getBean("sysUserService");
        SysUser sysUser = new SysUser();
        sysUser.setRealName("李");
        sysUser.setRoleId(3l);
        List<SysUser> userList=sysUserService.getUserList(sysUser);
        for (SysUser user : userList) {
            logger.info("SysUser: account={}, realName={}, roleName={}",
                    user.getAccount(), user.getRealName(),
                    user.getSysRole().getRoleName());
        }
    }

    /**
     * 第三章实战1、2
     */
    @Test
    public void test2() {
        ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
        SupplierService supplierService = (SupplierService) context.getBean("supplierService");
        Supplier supplier=new Supplier();
        supplier.setSupName("科技");
        List<Supplier> list=supplierService.selectSupname(supplier);
        for (Supplier sup : list) {
            System.out.println(sup.toString());
        }
    }

    /**
     * 第3章实战4：增删改
     */
    @Test
    public void insert() {
        ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
        SysUserService sysUserService = (SysUserService) context.getBean("sysUserService");
        SysUser user1 = new SysUser();
        user1.setAccount("ceshi1");
        user1.setRealName("测试用户1");
        sysUserService.insert(user1);
    }

    @Test
    public void update() {
        ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
        SysUserService sysUserService = (SysUserService) context.getBean("sysUserService");
        SysUser user1 = new SysUser();
        user1.setAccount("ceshi2");
        user1.setRealName("测试用户2");
        user1.setId(24l);
        sysUserService.update(user1);
    }

    @Test
    public void delete() {
        ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
        SysUserService sysUserService = (SysUserService) context.getBean("sysUserService");
        sysUserService.delete(24);
    }

    /**
     * 第3章实战5：根据ID做修改、删除操作
     */
    @Test
    public void Supplieupdate() {
        ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
        SupplierService supplierService = (SupplierService) context.getBean("supplierService");
        Supplier supplier = new Supplier();
        supplier.setSupName("12");
        supplier.setSupDesc("1323456");
        supplier.setSupContact("12345");
        supplier.setSupAddress("gyhujhj");
        supplier.setId(115);
        supplierService.update(supplier);
    }

    @Test
    public void Suppliedelete() {
        ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
        SupplierService supplierService = (SupplierService) context.getBean("supplierService");
        supplierService.delete(115);
    }

    /**
     * 第3章实战6：入库单管理模块增删改
     */
    @Test
    public void StorageInsert() throws ParseException {
        ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
        StorageRecordService storageRecordService = (StorageRecordService) context.getBean("storageRecordService");
        StorageRecord storageRecord=new StorageRecord();
        storageRecord.setGoodsName("维他奶");
        storageRecord.setGoodsDesc("123");
        storageRecord.setGoodsUnit("瓶");
        storageRecord.setGoodsCount(10);
        storageRecord.setTotalAmount(20);
        storageRecord.setPayStatus(2);
        Date date=new SimpleDateFormat("yyyy-MM-dd hh:mm:ss").parse("2024-12-11 00:00:00");
        storageRecord.setCreatedTime(date);
        storageRecord.setUpdatedUserId(2);
        storageRecord.setUpdatedTime(new Date());
        storageRecordService.insert(storageRecord);
    }

    @Test
    public void StorageUpdate() throws ParseException {
        ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
        StorageRecordService storageRecordService = (StorageRecordService) context.getBean("storageRecordService");
        StorageRecord storageRecord=new StorageRecord();
        storageRecord.setGoodsName("维他奶2");
        storageRecord.setGoodsDesc("12");
        storageRecord.setGoodsUnit("瓶2");
        storageRecord.setGoodsCount(101);
        storageRecord.setTotalAmount(202);
        storageRecord.setPayStatus(1);
        Date date=new SimpleDateFormat("yyyy-MM-dd hh:mm:ss").parse("2024-12-11 00:00:00");
        storageRecord.setCreatedTime(date);
        storageRecord.setUpdatedUserId(2);
        storageRecord.setUpdatedTime(new Date());
        storageRecord.setId(25);
        storageRecordService.update(storageRecord);
    }

    @Test
    public void StorageDelete() throws ParseException {
        ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
        StorageRecordService storageRecordService = (StorageRecordService) context.getBean("storageRecordService");
        storageRecordService.delete(25);
    }
}
